mod ws;
mod http_api;
mod util;
mod model;
mod satori_api;
mod security;

use crate::ws::{AppState, SenderData};
use tokio::sync::mpsc;
use void_log::log_msg;

async fn run() {
    let (sender_tx, sender_rx) = mpsc::channel::<SenderData>(1000);
    let app_state = AppState::new(sender_tx).await;

    let mut sender_task = tokio::spawn(app_state.clone().send_task(sender_rx));
    let mut server_task = tokio::spawn(app_state.server());

    tokio::select! {
        _ = &mut sender_task => {
            log_msg!("Sender stopped");
            server_task.abort();
        }
        _ = &mut server_task => {
            log_msg!("Server stopped");
            sender_task.abort();
        }
        _ = tokio::signal::ctrl_c() => {
            log_msg!("Ctrl C detected");
            server_task.abort();
            sender_task.abort();
        }
    }
}

#[tokio::main]
async fn main() {
    run().await;
}